package com.example.coinanimdemo

import android.os.Bundle
import android.util.Log
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.animation.core.Animatable
import androidx.compose.animation.core.LinearEasing
import androidx.compose.animation.core.tween
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.material3.Button
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import com.example.topbarDemp.theme.TopBarDemoTheme
import kotlin.math.cos
import kotlin.math.sin
import kotlin.random.Random

// https://juejin.cn/post/7412819188853309503?utm_source=gold_browser_extension

@OptIn(ExperimentalMaterial3Api::class)
class MainActivity : ComponentActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContent {
            TopBarDemoTheme {
                Scaffold { paddingValues ->
                    Greeting(modifier = Modifier.padding(paddingValues))
                }
            }
        }
    }
}

@Composable
fun Greeting(modifier: Modifier = Modifier) {
    var show by remember {
        mutableStateOf(false)
    }

    Box(modifier = Modifier.fillMaxSize()) {
        Button(modifier = Modifier.align(Alignment.Center), onClick = {
            show = !show
        }) {
            Text(text = "点击测试动画")
        }

        if (show) {
            Box(modifier = Modifier.align(Alignment.Center)) {
                CreateAnimation()
            }
        }
    }

}


@Composable
fun CreateAnimation(modifier: Modifier = Modifier) {

    val expansionProgress = remember {
        Animatable(0f)
    }

    val circleCount = 20

    val randomDistances = remember {
        List(circleCount) { Random.nextFloat() * 100 + 200 }
    }

    val angles = remember {
        List(circleCount) { it * (360f / circleCount) * (Math.PI / 180f).toFloat() }
    }

    Log.e("TAG", "randomDistances = $randomDistances  angles = $angles", )

    LaunchedEffect(key1 = Unit) {
        expansionProgress.animateTo(
            targetValue = 1f,
            animationSpec = tween(durationMillis = 300, easing = LinearEasing)
        )
    }

    for (i in 0 until  circleCount) {
        val distance = randomDistances[i] * expansionProgress.value
        val offsetX = cos(angles[i]) * distance
        val offsetY = sin(angles[i]) * distance

        Image(
            modifier = modifier
                .size(32.dp)
                .offset(pxToDp(px = offsetX), pxToDp(px = offsetY)),
            painter = painterResource(id = R.drawable.coin),
            contentDescription = "coin",
        )
    }
}


@Composable
fun pxToDp(px: Float): Dp {
    // 获取 LocalDensity 实例
    val density = LocalDensity.current
    // 使用 density 转换 px 为 dp
    return with(density) { px.toDp() }
}
